package com.metaera.app.ui.activities;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.jaeger.library.StatusBarUtil;
import com.metaera.app.R;
import com.metaera.app.controller.core.IPaginationPresenter;
import com.metaera.app.controller.core.SimplePaginationPresenter;
import com.metaera.app.controller.view.BasicPaginationView;
import com.metaera.app.controller.view.RefreshLoadMoreView;
import com.metaera.app.controller.view.core.IPaginationView;
import com.metaera.app.data.DataRepository;
import com.metaera.app.data.model.ArticleListResponse;
import com.metaera.app.data.model.BasicListResponse;
import com.metaera.app.data.network.ApiParam;
import com.metaera.app.ui.adapters.HomeNewsAdapter;
import com.metaera.app.ui.core.BaseRequestActivity;
import com.metaera.app.utils.ViewUtils;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;

import and.fast.statelayout.FailureStateView;
import androidx.recyclerview.widget.RecyclerView;
import io.reactivex.Observable;

public class LabelNewsListActivity extends BaseRequestActivity {

    public static Intent newInstance(Context context, int id) {
        Intent intent = new Intent(context, LabelNewsListActivity.class);
        intent.putExtra("ID", id);
        return intent;
    }

    @Override
    protected void initStatusBar() {
        StatusBarUtil.setTranslucentForImageViewInFragment(this, 0, null);
        StatusBarUtil.setLightMode(this);
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_tag_news_list;
    }

    @Override
    protected void initialize(Bundle savedInstanceState) {
        ViewUtils.addStatusBarHeightPadding(findViewById(R.id.layout_title_container));

        // find
        mStateLayout = findViewById(R.id.state_layout);
        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        SmartRefreshLayout refreshLayout = findViewById(R.id.smart_refresh_layout);

        // init
        recyclerView.setAdapter(new HomeNewsAdapter());
        mStateLayout.addStateView(new FailureStateView(R.layout.view_state_layout_failure)); // 错误布局

        // network
        IPaginationPresenter<IPaginationView<ArticleListResponse>, ArticleListResponse>
                presenter = createPresenter();
        BasicPaginationView<ArticleListResponse> paginationView =
                new BasicPaginationView<>(recyclerView, refreshLayout, this);
        paginationView.setEmptyLayoutResId(R.layout.view_state_layout_empty); // 空内容
        paginationView.setNotMoreFooterLayoutResId(R.layout.view_list_not_more); // 没有更多
        presenter.setModelAndView(paginationView);
        presenter.requestRefresh();

        // listener
        refreshLayout.setOnRefreshLoadMoreListener(new RefreshLoadMoreView<>(presenter));
    }

    private IPaginationPresenter<IPaginationView<ArticleListResponse>, ArticleListResponse> createPresenter() {
        int id = getIntent().getIntExtra("ID", 0);

        return new SimplePaginationPresenter<ArticleListResponse>() {
            @Override
            protected Observable<BasicListResponse<ArticleListResponse>> execute() {
                return DataRepository.getInstance().getLabelDetails(id, mPage, ApiParam.PAGE_SIZE);
            }
        };
    }

}
